package interactive.states
{
	import flash.events.TimerEvent;
	import flash.geom.Point;
	
	import global.Managers;
	
	import interactive.GestureManager;
	
	import utils.EnterFrameTimer;
	import utils.pool.ObjectPool;

	/**
	 *
	 * 等待停止移动状态
	 * 1. 如果与接触点距离小于手势最小距离，则变为单击状态
	 * 2. 当等待时间大于离开延迟计时时变为离开状态
	 * 3. 当等待时间不大于离开延迟计时并且触摸了屏幕，则返回移动状态
	 *  
	 * @author Raykid
	 * 
	 */
	public class WaitStopMoveState implements IGestureState
	{
		private var _lastPoint				: Point;
		private var _timerLeave				: EnterFrameTimer;
		
		public function WaitStopMoveState()
		{
			_lastPoint = new Point();
			
			_timerLeave = new EnterFrameTimer(GestureManager.LEAVE_DELAY, 1);
			_timerLeave.addEventListener(TimerEvent.TIMER_COMPLETE, timerLeaveCompleteHandler);
		}
		private function timerLeaveCompleteHandler(event:TimerEvent):void {
			var manager:GestureManager = Managers.gestureManager;
			var point:Point = ObjectPool.pop(Point) as Point;
			point.x = manager.touchX;
			point.y = manager.touchY;
			if(Point.distance(_lastPoint, point) <= GestureManager.HAND_DISTANCE_MIN) {
				// 1. 如果与接触点距离小于手势最小距离，则变为单击状态
				manager.switchState(manager.clickState, point);
			} else {
				// 2. 当等待时间大于离开延迟计时时变为离开状态
				manager.switchState(manager.leaveState, _lastPoint);
			}
			ObjectPool.push(point);
		}

		public function backIn(manager:GestureManager, point:Point):void
		{
			// 无操作
		}
		public function switchIn(manager:GestureManager, point:Point):void
		{
			_lastPoint.x = point.x;
			_lastPoint.y = point.y;
			_timerLeave.start();
		}
		public function switchOut(manager:GestureManager, point:Point):void
		{
			_timerLeave.reset();
		}
		public function touch(manager:GestureManager, point:Point):void
		{
			// 3. 当等待时间不大于离开延迟计时并且触摸了屏幕，则返回移动状态
			manager.backToState(manager.moveState, point);
		}
		public function leave(manager:GestureManager, point:Point):void
		{
			// 无操作
		}
		public function move(manager:GestureManager, point:Point):void
		{
			// 无操作
		}
		public function toString():String {
			return "WaitStopMoveState";
		}
	}
}